import { defineStore, storeToRefs } from 'pinia'
import { useAqiStore } from '@/stores/aqi/aqi.js'
import $ajax from '@/api/to8040.js'
import { showErrorMsg, showSuccessMsg } from '@/utils/alterMessage.js'

export const useStatisticsStore = defineStore('statistics', {
  state: () => ({
    statistic: {
      province: '',
      address: '',
      so2Value: 0.0,
      coValue: 0.0,
      spmValue: 0.0,
      gmId: '',
      fdId: '',
      information: '',
      remark: ''
    },
    isShow: false,
    drawerVisible: false
  }),
  getters: {
    so2Level() {
      const { so2MinNums, so2MaxNums } = storeToRefs(useAqiStore())
      // console.log(so2MinNums.value, so2MaxNums.value)
      for (let i = 0; i < so2MinNums.value.length; i++) {
        // 注意这里的修改，使用.value来访问
        if (
          so2MinNums.value[i] <= this.statistic.so2Value &&
          this.statistic.so2Value <= so2MaxNums.value[i]
        ) {
          return i + 1
        }
      }
      return 6
    },
    coLevel() {
      const { coMinNums, coMaxNums } = storeToRefs(useAqiStore())
      for (let i = 0; i < coMinNums.value.length; i++) {
        if (
          coMinNums.value[i] <= this.statistic.coValue &&
          this.statistic.coValue <= coMaxNums.value[i]
        ) {
          return i + 1
        }
      }
      return 6
    },
    spmLevel() {
      const { spmMinNums, spmMaxNums } = storeToRefs(useAqiStore())
      for (let i = 0; i < spmMinNums.value.length; i++) {
        if (
          spmMinNums.value[i] <= this.statistic.spmValue &&
          this.statistic.spmValue <= spmMaxNums.value[i]
        ) {
          return i + 1
        }
      }
      return 6
    },
    aqiId() {
      const { aqiMinNums, aqiMaxNums } = storeToRefs(useAqiStore())
      const aqiValue = Math.max(this.so2, this.co, this.spm)
      for (let i = 0; i < aqiMinNums.value.length; i++) {
        if (aqiMinNums.value[i] <= aqiValue && aqiValue <= aqiMaxNums.value[i]) {
          return i + 1
        }
      }
      return 6
    },
    so2() {
      const level = this.so2Level - 1
      const { so2MinNums, so2MaxNums, aqiMinNums, aqiMaxNums } = storeToRefs(useAqiStore())
      return (
        ((aqiMaxNums.value[level] - aqiMinNums.value[level]) *
          (this.statistic.so2Value - so2MinNums.value[level])) /
          (so2MaxNums.value[level] - so2MinNums.value[level]) +
        aqiMinNums.value[level]
      )
    },
    co() {
      const level = this.coLevel - 1
      const { coMinNums, coMaxNums, aqiMinNums, aqiMaxNums } = storeToRefs(useAqiStore())
      return (
        ((aqiMaxNums.value[level] - aqiMinNums.value[level]) *
          (this.statistic.coValue - coMinNums.value[level])) /
          (coMaxNums.value[level] - coMinNums.value[level]) +
        aqiMinNums.value[level]
      )
    },
    spm() {
      const level = this.spmLevel - 1
      const { spmMinNums, spmMaxNums, aqiMinNums, aqiMaxNums } = storeToRefs(useAqiStore())
      return (
        ((aqiMaxNums.value[level] - aqiMinNums.value[level]) *
          (this.statistic.spmValue - spmMinNums.value[level])) /
          (spmMaxNums.value[level] - spmMinNums.value[level]) +
        aqiMinNums.value[level]
      )
    }
  },
  actions: {
    clear() {
      this.statistic = {
        province: '',
        address: '',
        so2Value: 0.0,
        coValue: 0.0,
        spmValue: 0.0,
        gmId: '',
        fdId: '',
        information: '',
        remark: ''
      }
    },
    clearForm() {
      this.statistic.so2Value = 0.0
      this.statistic.coValue = 0.0
      this.statistic.spmValue = 0.0
      this.statistic.information = ''
      this.statistic.remark = ''
    },
    async updateStatistic() {
      this.statistic.so2Level = this.so2Level
      this.statistic.coLevel = this.coLevel
      this.statistic.spmLevel = this.spmLevel
      this.statistic.aqiId = this.aqiId
      if (this.statistic.so2Value === '') {
        showErrorMsg('请输入SO2浓度')
        return
      }
      if (this.statistic.coValue === '') {
        showErrorMsg('请输入CO浓度')
        return
      }
      if (this.statistic.spmValue === '') {
        showErrorMsg('请输入PM2.5浓度')
        return
      }
      if (this.statistic.information === '') {
        showErrorMsg('请输入描述信息')
        return
      }
      const path = '/statistics/submit'
      // console.log(this.statistic)
      const res = await $ajax.post(path, this.statistic)
      console.log(res)
      if (res != null && res.code != null && res.code === 200) {
        showSuccessMsg('提交成功')
        return true
      } else {
        showErrorMsg('提交失败')
        return false
      }
    },
    async getStatistic(afId) {
      const path = '/statistics/select/ByAfId'
      const res = await $ajax.get(path, { params: { afId: afId } })
      if (res != null && res.code != null && res.code === 200) {
        return res.result
      } else {
        showErrorMsg('获取数据失败')
        return null
      }
    }
  }
})
